package controller;

import java.io.IOException;

import filesystem.GlobalFSData;
import filesystem.model.File;
import filesystem.service.DiskService;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class AttributeController extends Application {
	@FXML
	private RadioButton readWrite;
	@FXML
	private RadioButton readOnly;

	@FXML
	private Label typeLabel;
	@FXML
	private Label size;
	@FXML
	private Label startIndex;
	@FXML
	private Label attribute;
	@FXML
	private AnchorPane propertyScene;

	private ToggleGroup group;
	private Stage primaryStage;
	private File file;

	private Boolean isWrite;


	public AttributeController(File file) {
		this.file = file;
		this.isWrite=!file.isReadOnly();
	}

	@Override
	public void start(Stage primaryStage) {
		this.primaryStage = primaryStage;
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/AttributeSetter.fxml"));
		loader.setController(this);
		try {
			loader.load();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.primaryStage.getIcons().add(new Image("/imgs/file.png",50,50,true,true));
		this.show();
		Scene scene = new Scene(this.propertyScene);
		this.primaryStage.setScene(scene);
		this.primaryStage.setResizable(false);
		this.primaryStage.setTitle("属性");
		this.primaryStage.show();

		this.primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			public void handle(WindowEvent windowEvent) {
				DiskService.save2Disk(GlobalFSData.getDisk(), main.Main.DISK, GlobalFSData.getFat());
				primaryStage.close();
			}
		}); // save file system if clicking the close button

	}


	public void show() {
		if (file.getAttribute() == File.DIRECTORY) {
			typeLabel.setText("文件夹");
			readWrite.setDisable(true);
			readOnly.setDisable(true);
		} else if (file.getAttribute() == File.FILE) {
			typeLabel.setText("文件");
		}

		size.setText(file.getFileSize() + "字节");
		startIndex.setText(String.valueOf(file.getStartIndex()));

		if(file.getType()== File.EXE) {
			attribute.setText("可执行文件");
		}
		else {
			attribute.setText("文本文件");
		}

		group = new ToggleGroup();

		readWrite.setToggleGroup(group);
		if (file.isReadOnly()) {
			readOnly.setSelected(true);
			readWrite.setSelected(false);
		} else {
			readOnly.setSelected(false);
			readWrite.setSelected(true);
		}
		readOnly.setToggleGroup(group);

		group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

			@Override
			public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
				if (((ToggleButton) newValue).getText().equals("读写")) {
					isWrite=true;
				} else {
					isWrite=false;
				}
			}
		});
	}


	@FXML
	void apply(ActionEvent event) {
		file.setReadOnly(!isWrite);
	}

	@FXML
	void cancel(ActionEvent event) {
		primaryStage.close();
	}

	@FXML
	void makeSure(ActionEvent event) {
		file.setReadOnly(!isWrite);
		primaryStage.close();
	}
}
